package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.query.StrategyCatalogQuery;
import cn.wolfcode.luowowo.article.query.TravelQuery;
import cn.wolfcode.luowowo.article.service.*;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/destination")
public class DestinationController {
    @Reference
    private IRegionService regionService;
    @Reference
    private IDestinationService destinationService;
    @Reference
    private IStrategyCatalogService strategyCatalogService;
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private ITravelService travelService;

    @RequestMapping("")
    public String index(Model model){
        //hotRegions
        List<Region> hotRegions = regionService.queryHotRegions();
        model.addAttribute("hotRegions",hotRegions);
        return "destination/index";
    }

    @RequestMapping("/getHotDestByRegionId")
    public String getHotDestByRegionId(@ModelAttribute("regionId") Long regionId,Model model){
        List<Destination> list = destinationService.getDestByRegionId(regionId);
        //列表数据需要左右两边分列显示
        int mid = list.size() / 2;
        List<Destination> leftDests = new ArrayList<>();
        List<Destination> rightDests = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i < mid){
                leftDests.add(list.get(i));
            }else{
                rightDests.add(list.get(i));
            }
        }
        //leftDests
        model.addAttribute("leftDests",leftDests);
        //rightDests
        model.addAttribute("rightDests",rightDests);
        return "destination/hotdestTpl";
    }

    @RequestMapping("/guide")
    public String guide(Model model,Long id){
        //toasts
        List<Destination> toasts = destinationService.getToasts(id);
        //dest
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",destination);
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(id);
        model.addAttribute("catalogs",catalogs);
        //strategyDetails
        List<StrategyDetail> strategyDetails = strategyDetailService.getViewnumTopThree(id);
        model.addAttribute("strategyDetails",strategyDetails);
        return "destination/guide";
    }

    @RequestMapping("/surveyPage")
    public String surveyPage(Model model, @ModelAttribute("qo") StrategyCatalogQuery qo){
        //toasts
        List<Destination> toasts = destinationService.getToasts(qo.getDestId());
        //dest
        Destination destination = toasts.remove(toasts.size() - 1);
        model.addAttribute("toasts",toasts);
        model.addAttribute("dest",destination);
        return "destination/survey";
    }

    @RequestMapping("/survey")
    public String survey(Model model,@ModelAttribute("qo") StrategyCatalogQuery qo){
        //catalogs
        //catalogs
        List<StrategyCatalog> catalogs = strategyCatalogService.getCatalogByDestId(qo.getDestId());
        model.addAttribute("catalogs",catalogs);
        //catalog
        StrategyCatalog catalog = null;
        if(catalogs != null && catalogs.size() > 0){
            for (StrategyCatalog c : catalogs) {
                if (qo.getCatalogId() == c.getId()){
                    catalog = c;
                    model.addAttribute("catalog",catalog);
                }
            }
        }
        //detail
        if (catalog != null){
            StrategyDetail strategyDetail = catalog.getDetails().get(0);
            StrategyContent content = strategyDetailService.getContent(strategyDetail.getId());
            strategyDetail.setStrategyContent(content);
            model.addAttribute("detail",strategyDetail);
        }
        return "destination/surveyTpl";
    }

    @RequestMapping("/travels")
    public String travels(Model model, @ModelAttribute("qo") TravelQuery qo){
        PageInfo pageInfo = travelService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "destination/travelTpl";
    }
}
